package Dao;

import Utils.JdbcTemplateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import pojo.Login;
import pojo.projects;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class LoginDao
{
    JdbcTemplate jdbcTemplate=JdbcTemplateUtils.getJdbcTemplate();
    public boolean Login_verify(String userName,String passWord)
    {
        String sql="select * from loginuser where username= ? and password=?";
        Object[] objects=new Object[2];
        objects[0]=userName;
        objects[1]=passWord;
        List<Map<String, Object>> users = jdbcTemplate.queryForList(sql,objects);
        if (users.isEmpty())
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public List<Login> showUserByUsername(String username){
        String sql="select * from loginuser where username='"+username+"'";
        List<Login> users = jdbcTemplate.query(sql, new RowMapper<Login>() {
            @Override
            public Login mapRow(ResultSet resultSet, int i) throws SQLException {
                Login login = new Login();
                login.setUserName(resultSet.getString("username"));
                login.setPassWord(resultSet.getString("password"));
                login.set_teacher(resultSet.getBoolean("isteacher"));
                login.setProject1(resultSet.getString("project1"));
                login.setProject2(resultSet.getString("project2"));
                return login;
            }
        });
        return users;
    }
    public List<Login> showUserById(int id){
        String sql="select * from loginuser where id='"+id+"'";
        List<Login> users = jdbcTemplate.query(sql, new RowMapper<Login>() {
            @Override
            public Login mapRow(ResultSet resultSet, int i) throws SQLException {
                Login login = new Login();
                login.setUserName(resultSet.getString("username"));
                login.setPassWord(resultSet.getString("password"));
                login.set_teacher(resultSet.getBoolean("isteacher"));
                login.setProject1(resultSet.getString("project1"));
                login.setProject2(resultSet.getString("project2"));
                return login;
            }
        });
        return users;
    }
    public List<Map<String,Object>> showUser(){
        String sql="select * from loginuser";
        List<Map<String, Object>> students = jdbcTemplate.queryForList(sql);
        return students;
    }
    public void addUser(Login login){
        String sql="INSERT into loginuser (username,password,isteacher,project1,project2) values (?,?,?,?,?)";
        Object[] objects=new Object[5];
        objects[0]=login.getUserName();
        objects[1]=login.getPassWord();
        objects[2]=login.is_teacher();
        objects[3]=login.getProject1();
        objects[4]=login.getProject2();
        jdbcTemplate.update(sql,objects);
        System.out.println("添加成功");
    }
    public void deleteLogin(Integer id)
    {
        String sql="DELETE FROM loginuser Where id="+id;
        jdbcTemplate.update(sql);
        System.out.println("删除成功");
    }

    public void updateLogin(projects projects,String username,Integer id){
        String sql="UPDATE loginuser SET project1=? WHERE username=?";
        String sql2="UPDATE loginuser SET project2=? WHERE username=?";
        Object[] objects=new Object[2];
        if (id==1)
        {
            if (showUserByUsername(username).get(0).getProject1().equals(""))
            {
                objects[0]=projects.getProjects();
                objects[1]=username;
                jdbcTemplate.update(sql,objects);
                System.out.println("添加课程1成功");
            }
            else
            {
                System.out.println("该课程已添加 请勿重新添加");
            }
        }
        else if(id==2)
        {
            if (showUserByUsername(username).get(0).getProject2().equals(""))
            {
                objects[0]=projects.getProjects();
                objects[1]=username;
                jdbcTemplate.update(sql2,objects);
                System.out.println("添加课程2成功");
            }else
            {
                System.out.println("该课程已添加 请勿重新添加");
            }
        }
    }
}
